package me.maiz.lab.batchdemo.xxljob;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

@Component
@Slf4j
public class WeeklyUpdateUserCreditJob {

    @Resource
    private JobLauncher jobLauncher;

    @Resource
    private Job userCalcLevelJob;

    public void startJob(){
        Date now = DateUtil.lastWeek();

        JobParameters jobParameters =
                new JobParametersBuilder()
                        .addDate("startTime", now)
                        .toJobParameters();

        JobExecution execution = null;
        try {
            execution = jobLauncher.run(userCalcLevelJob, jobParameters);

            //TODO 异常处理
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }
        System.out.println("STATUS :: " + execution.getStatus());
    }


}
